package com.clsz.controller;

import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/mq")
public class MQController {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    /**
     * 同步演示
     * @return
     */
    @GetMapping("/synchronize")
    public Object synchronize() throws InterruptedException {
        //模拟耗时任务
        System.out.println("==============>模拟耗时任务");
        for (int i = 0; i < 10; i++) {
            System.out.println("===>任务执行度："+(i+1)+"/10");
            Thread.currentThread().sleep(1000);
        }
        System.out.println("end");

        Map<String, Object> res = new HashMap<>();
        res.put("code", "200");
        res.put("message","ok");

        return res;
    }

    /**
     * 异步演示
     * @return
     */
    @GetMapping("/asynchronize")
    public Object asynchronize() throws InterruptedException {
        //发送消息mq的test02
        kafkaTemplate.send("test02", "i am key", "i am value");

        Map<String, Object> res = new HashMap<>();
        res.put("code", "200");
        res.put("message","ok");

        return res;
    }
}
